<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/13
 * author : weblinuxgame
 * filename: ApiResponse.php
 */

namespace App\Api\Responses;

use App\Core\Traits\Api\ApiResponse as Helper;
use Illuminate\Contracts\Support\Arrayable;
use OpenApi\Annotations as OA;


/**
 * @OA\Schema(
 *      schema="ApiResponse",
 *     type="object",
 *     description="响应实体，响应结果统一使用该结构",
 *     title="ApiResponse",
 * )
 * Class ApiResponse
 * @package App\Api\Responses
 */
class ApiResponse implements Arrayable
{
    use Helper;

    /**
     * @OA\Property(property="code",type="string",description="响应代码,异常码")
     * @var int
     */
    public $code;

    /**
     * @OA\Property(property="message", type="string", description="响应结果提示")
     * @var string
     */
    public $message;

    /**
     * @OA\Property(property="data", type="object", description="响应结果内容,无内容或者逻辑符时为null")
     * @var object
     */
    public $data;

    public function __construct($data = [], $message = 'ok', $code = 0)
    {
        $this->data = $data;
        $this->message = $message;
        $this->code = $code;
    }

    /**
     * 数据
     * @return array
     */
    public function toArray(): array
    {
        return [
            'data' => $this->data,
            'message' => $this->message,
            'code' => $this->code,
        ];
    }
}
